package org.jeecg.common.util;

import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ReflectUtil;
import lombok.experimental.UtilityClass;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * BeanUtil增强
 *
 * @author zhoutong
 */
@UtilityClass
public class BeanUtil {

    public static <T> List<T> copyToList(Collection<?> collection, Class<T> targetType) {
        if (null == collection) {
            return null;
        }
        if (collection.isEmpty()) {
            return new ArrayList<>(0);
        }
        return collection.stream().map((source) -> {
            final T target = ReflectUtil.newInstanceIfPossible(targetType);
            cn.hutool.core.bean.BeanUtil.copyProperties(source, target, CopyOptions.create());
            return target;
        }).collect(Collectors.toList());
    }

}
